package com.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.demo.annotation.PassToken;
import com.demo.annotation.UserLoginToken;
import com.demo.domain.UserJwt;
import com.demo.service.UserJwtService;
import com.demo.util.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;


/**
 * @Author： yu.Tan
 * @Date: 2020/8/28 10:26
 * @Description:
 */
@RestController
@RequestMapping("/user_jwt")
public class UserJwtController {
    @Autowired
    UserJwtService userJwtService;

    @PostMapping("/login")
    public Object loginA(@RequestBody UserJwt userJwt) {
        JSONObject jsonObject = new JSONObject();
        UserJwt userJwtA = userJwtService.findByUsername(userJwt);

        if (Objects.isNull(userJwtA)) {
            jsonObject.put("message", "登录失败,用户不存在");
            return jsonObject;
        } else {
            if (!userJwtA.getPassWord().equals(userJwt.getPassWord())) {
                jsonObject.put("message", "登录失败,密码错误");
                return jsonObject;
            } else {
                String token = TokenUtils.getToken(userJwtA);
                jsonObject.put("token", token);
                jsonObject.put("user", userJwtA);
                return jsonObject;
            }
        }
    }


    @PostMapping("/loginB")
    @UserLoginToken
    public UserJwt loginB(@RequestBody UserJwt userJwt) {
        return userJwtService.findByUsername(userJwt);
    }

    @PostMapping("/loginC")
    @PassToken
    public UserJwt loginC(@RequestBody UserJwt userJwt) {
        return userJwtService.findByUsername(userJwt);
    }

    @GetMapping("/cors_a")
    public String corsA() {
        return "跨域测试A";
    }


}
